# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
# This source file is part of the Cangjie project, licensed under Apache-2.0
# with Runtime Library Exception.
#
# See https://cangjie-lang.cn/pages/LICENSE for license information.


from os import path

integer_types = ['Int8', 'Int16', 'Int32', 'Int64', 'UInt8', 'UInt16', 'UInt32', 'UInt64']
other_types = ['Float16', 'Float32', 'Float64', 'String', 'Rune', 'Bool', 'Unit', '(Int8, Int8)', 'Array<Int8>', 'C']
types = integer_types + other_types
default_value_map = {'Bool' : 'true', 'Unit' : '()', 'Array<Int8>' : '[]', 'Rune' : "'1'", 'String' : '"1"', '(Int8, Int8)' : '(1, 1)', 'C' : 'C()'}
def default_value(ty : str) -> str:
  if ty in integer_types: return '1'
  if 'Float' in ty: return '1.0'
  return default_value_map[ty]

dir = path.dirname(path.realpath(__file__))
path = dir + '/test_' + path.basename(dir) + '_{}.cj'
negative_template = '''
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * This source file is part of the Cangjie project, licensed under Apache-2.0
 * with Runtime Library Exception.
 *
 * See https://cangjie-lang.cn/pages/LICENSE for license information.
 */

/*
  @Assertion:   4.21(3) The coalescing operator is used for unwrapping values of Option types. Suppose the type of the
                expression e1 is Option<T>, then we have following rules about the expression e1 ?? e2.
                1.The type of the expression e2 must be T.
  @Description: Checks that using operator ?? with types Option<{t1}> and {t2} is not permitted.
  @Mode: compileonly
  @Negative: yes
  @Structure: single
  @CompileWarning: no
  @Comment: Auto-generated by gen.py
*/
{}
main() {{
    var v1 : Option<{t1}> = Some({})
    var v2 : {t2} = {}
    let v3 = v1 ?? v2
}}
'''
class_decl = '\nclass C {}\n'
counter = 2

def write_counted(contents : str):
  global counter
  with open(path.format(str(counter).zfill(3)), 'w') as file:
    file.write(contents)
    counter += 1

negative_tests = [negative_template.format(class_decl if 'C' in (t1, t2) else '', default_value(t1), default_value(t2), t1=t1, t2=t2)
                  for t1 in types for t2 in types if t1 != t2]
for test in negative_tests:
  write_counted(test)
